package com.moses.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
public class Main {

    public static void main(String[] args)  {
        try {
            Class cls = Class.forName("com.moses.reflect.Person");

            Constructor constructor = cls.getConstructor(String.class);
            Person obj2 = (Person)constructor.newInstance("moses");
            Constructor[] constructors = cls.getConstructors();
            for (Constructor constr : constructors){
                System.out.println(constr.getName());
                Parameter[] parameters = constr.getParameters();
                for (Parameter param :parameters){
                    System.out.println(param.getType().getName());
                }
            }

            for (Field field : cls.getFields()){
                System.out.println(field.getName());
            }
            System.out.println(cls.toString());
            System.out.println("================");
            for (Field field: cls.getDeclaredFields()){
                System.out.println(field.getName());
                field.setAccessible(true);
                field.set(cls, "test");
            }
            System.out.println(cls.toString());
        }catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
